package cn.edu.xmu.restfuldemo.dao;

import cn.edu.xmu.restfuldemo.mapper.OrderItemPoMapper;
import cn.edu.xmu.restfuldemo.model.BO.OrderItem;
import cn.edu.xmu.restfuldemo.model.BO.Orders;
import cn.edu.xmu.restfuldemo.model.PO.OrderItemPo;
import cn.edu.xmu.restfuldemo.model.PO.OrderItemPoExample;
import cn.edu.xmu.restfuldemo.model.VO.OrderItemVo;
import cn.edu.xmu.restfuldemo.util.ResponseCode;
import cn.edu.xmu.restfuldemo.util.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Team3-5
 */
@Repository
public class OrderItemDao {
    @Autowired
    OrderItemPoMapper orderItemPoMapper;

    /**
     * 根据orderId查找orderItem
     * @param orderId orderOd
     * @return returnObject
     */
    public ReturnObject<List<OrderItem>> findOrderItemListByOrderId(Long orderId){
        OrderItemPoExample example = new OrderItemPoExample();
        OrderItemPoExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<OrderItemPo>orderItemPos = orderItemPoMapper.selectByExample(example);
        List<OrderItem>orderItems = new ArrayList<>();
        for(OrderItemPo orderItemPo:orderItemPos){
            orderItems.add((new OrderItemVo(orderItemPo)).newOrderItem());
        }
        return new ReturnObject<>(orderItems);
    }

    /**
     * 插入orders
     * @param orderItem orderItem
     * @return returnObject
     */
    public ReturnObject addOrderItem(OrderItem orderItem){
        orderItemPoMapper.insert(orderItem.newOrderItemPo());
        return new ReturnObject(ResponseCode.OK,"成功");
    }
}
